package com.lx.base;


import com.lx.base.aop.BizLogger;
import com.lx.base.api.dto.BaseDto;
import com.lx.base.api.param.PageBaseParam;
import com.lx.base.exception.CommonError;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * @author lixing
 * @date 2022/7/25 16:51
 */
@Slf4j
public class BaseController<X extends BaseService,DTO,ADD,UPDATE,PAGEPARAM extends PageBaseParam> {
    @Autowired
    protected X baseservice;


    @ApiOperation(value = "根据id查询")
    @PostMapping(value="/findById")
    @ResponseBody
    @BizLogger
    public BaseDto<DTO> findById(@RequestParam("id") java.io.Serializable id ) {
        try {
            return baseservice.findById(id);

        } catch (Exception e) {
            log.error("根据id获取数据异常:",e);
            return BaseDto.fail(CommonError.SYSTEM_ERROR);
        }

    }
    @ApiOperation(value = "根据id删除数据")
    @GetMapping(value="/delById")
    @ResponseBody
    @BizLogger
    public BaseDto delById(@RequestParam("id") java.io.Serializable id ) {
        try {
            baseservice.removeById(id);
            return BaseDto.success("");
        } catch (Exception e) {
            log.error("根据id删除数据异常:",e);
            return BaseDto.fail(CommonError.SYSTEM_ERROR);
        }

    }

    @ApiOperation(value = "根据ids批量删除数据")
    @PostMapping(value="/delByIds")
    @ResponseBody
    @BizLogger
    public BaseDto delByIds(@RequestBody List<Serializable> ids ) {
        try {
            baseservice.removeByIds(ids);
            return BaseDto.success("");
        } catch (Exception e) {
            log.error("根据id删除数据异常:",e);
            return BaseDto.fail(CommonError.SYSTEM_ERROR);
        }

    }


    @ApiOperation(value = "新增数据")
    @PostMapping(value="/save")
    @ResponseBody
    @BizLogger
    public BaseDto<DTO> save(@RequestBody @Valid ADD add, BindingResult bindingResult) {
        try {
           if(bindingResult.hasErrors()){
               return BaseDto.fail(CommonError.INVALID_ARG,bindingResult.getAllErrors().get(0).getDefaultMessage());
           }
           return baseservice.add(add);
        } catch (Exception e) {
            log.error("新增数据异常:",e);
            return BaseDto.fail(CommonError.SYSTEM_ERROR);
        }

    }
    @ApiOperation(value = "更新数据")
    @PostMapping(value="/update")
    @ResponseBody
    @BizLogger
    public BaseDto<DTO> update(@RequestBody @Valid UPDATE updateparam, BindingResult bindingResult) {
        try {
            if(bindingResult.hasErrors()){
                return BaseDto.fail(CommonError.INVALID_ARG,bindingResult.getAllErrors().get(0).getDefaultMessage());
            }
            return baseservice.updateByParam(updateparam);
        } catch (Exception e) {
            log.error("更新数据异常:",e);
            return BaseDto.fail(CommonError.SYSTEM_ERROR);
        }
    }

    @ApiOperation(value = "分页查询")
    @PostMapping(value="/pagelist")
    @ResponseBody
    @BizLogger
    public BaseDto<List<DTO>> pagelist(@RequestBody PAGEPARAM pageparam) {
        try {
            return baseservice.pagelist(pageparam);
        } catch (Exception e) {
            log.error("分页查询:",e);
            return BaseDto.fail(CommonError.SYSTEM_ERROR);
        }

    }
}
